/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BACustomNavigationController.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 06/08/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BACustomNavigationController.h"
#import "BAMaterialViewController.h"
#import "BASpecialNavigationBarConfigurationDelegate.h"
#import "BALibrarySettings.h"
#import "BAUtils.h"


@interface BACustomNavigationController ()

@end

@implementation BACustomNavigationController

/* NB: THIS IS NEVER USED due to the special instantiation call type
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
       
    }
    return self;
}
*/

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if ([self.navigationBar isKindOfClass:[BACustomNavigationBar class]]) {
        self.customNavigationBar = (BACustomNavigationBar*)self.navigationBar;
        if ([self.viewControllers count]>0) {
            [self checkNavBarColorForViewController:[self.viewControllers lastObject] animated:NO];
        }
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotate{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations{
    if (RD_IS_IPAD) {
        return UIInterfaceOrientationMaskLandscape;
    } else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    BOOL animateColorChange = animated;
    if (self.navigationBarHidden) {
        animateColorChange = NO;
    }
    
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 6, 32, 32)];
    
#ifdef BA_IMF_BUILD
    if ([viewController respondsToSelector:@selector(handleCutsomNavigationControllerBackButtonClicked)]) {
        [button addTarget:viewController action:@selector(handleCutsomNavigationControllerBackButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    } else
#endif
    {
        [button addTarget:self action:@selector(customBackButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    UIBarButtonItem *backButtonItem;
    if ([self getNavigationBarConfiguration:viewController]==BASpecialNavigationBarConfigurationCompletelyColored) {
        [button setImage:[BAUtils instance].whiteBackButtonImage forState:UIControlStateNormal];
        backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    } else {
        [button setImage:[BAUtils instance].grayBackButtonImage forState:UIControlStateNormal];
        UIView* displacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 38)]; //displace because of top nav bar color bar
        [displacerView addSubview:button];
        backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:displacerView];
    }
    
    
    
    if ([[BAUtils instance] iOSMajorVersion] > 6) {
        // Hack to position button on iOS 7.
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        item.width = -12;
        [viewController.navigationItem setLeftBarButtonItems:@[item, backButtonItem]];
    }
    else {
        viewController.navigationItem.leftBarButtonItem = backButtonItem;
    }
    
    [super pushViewController:viewController animated:animated];
    
    [self checkNavBarColorForViewController:viewController animated:animateColorChange];
}

-(UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    UIViewController* viewController = [super popViewControllerAnimated:animated];
    
    [self checkNavBarColorForTopMostViewControllerAnimated:animated];
    
    return viewController;
}

-(NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSArray* viewControllers = [super popToViewController:viewController animated:animated];

    [self checkNavBarColorForTopMostViewControllerAnimated:animated];

    return viewControllers;
}

-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{
    NSArray* viewControllers = [super popToRootViewControllerAnimated:animated];
    
    [self checkNavBarColorForTopMostViewControllerAnimated:animated];
    
    return viewControllers;
}

-(BASpecialNavigationBarConfiguration)getNavigationBarConfiguration:(UIViewController*)viewController
{
    if ([viewController conformsToProtocol:@protocol(BASpecialNavigationBarConfigurationDelegate)]) {
        return [(id<BASpecialNavigationBarConfigurationDelegate>)viewController getRequestedSpecialNavigationBarConfiguration];
    } else {
        return BASpecialNavigationBarConfigurationDefault;
    }

}

-(void)checkNavBarColorForViewController:(UIViewController*)viewController animated:(BOOL)animated
{
    BASpecialNavigationBarConfiguration c = [self getNavigationBarConfiguration:viewController];
    
    if (c == BASpecialNavigationBarConfigurationCompletelyColored) {
        [self.customNavigationBar changeToFullCustomerColorAnimated:animated];
    } else {
        [self.customNavigationBar changeToTopOnlyCustomerColorAnimated:animated];
    }

}

-(void)checkNavBarColorForTopMostViewControllerAnimated:(BOOL)animated
{
    if ([self.viewControllers count]>0) {
        [self checkNavBarColorForViewController:[self.viewControllers lastObject] animated:animated];
    }
}

-(void)setViewControllers:(NSArray *)viewControllers
{
    [super setViewControllers:viewControllers];
    [self checkNavBarColorForTopMostViewControllerAnimated:NO];
}

-(void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
{
    [super setViewControllers:viewControllers animated:animated];
    [self checkNavBarColorForTopMostViewControllerAnimated:animated];
}

- (void)customBackButtonClicked
{
    [self popViewControllerAnimated:YES];
}

@end
